package com.keyin.widget;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.keyin.R;
import com.keyin.protocol.CacheImageProtocol;
import com.keyin.protocol.DownloadMagazine;
import com.keyin.protocol.NewPeriodicals;
import com.keyin.protocol.Periodical;
import com.keyin.protocol.Protocol;
import com.keyin.protocol.Protocol.OnJsonProtocolResult;
import com.keyin.protocol.ProtocolHelper;

public class NewPeriodicalsWidget extends MainWidgetBase {

	private View magazines;

	private ProgressDialog progressDialog;
	private NewPeriodicals newMagazines;

	private Bitmap[] bitmaps;
	
	private BaseAdapter adapter;

	public NewPeriodicalsWidget(Activity context) {
		super(context);
	}

	@Override
	public void onNewIntent(Intent i) {
		if (magazines == null) {
			magazines = View.inflate(getContext(), R.layout.v_new_magazine,
					null);
			addView(magazines);

			progressDialog = ProgressDialog.show(parent, "请稍候", "正在获取数据", true);
			progressDialog.setCancelable(true); 
			progressDialog.setProgressStyle(android.R.style.Theme_Translucent);
			progressDialog.setOnCancelListener(new OnCancelListener() {
				@Override
				public void onCancel(DialogInterface d) {
				}
			});

			Protocol p = ProtocolHelper.getNewMagazines(getContext());
			p.startTrans(new OnJsonProtocolResult(NewPeriodicals.class) {

				@Override
				public void onResult(Object o) {
					handler.sendMessage(Message.obtain(handler, 1, o));
				}

				@Override
				public void onException(Exception e) {
					handler.sendEmptyMessage(2);
					e.printStackTrace();
				}
			});
		}
	}

	private void refreshUI(NewPeriodicals nm) {
		progressDialog.dismiss();
		newMagazines = nm;

		int size = newMagazines.magazines.size();
		size = size > 6 ? 6 : size;
		bitmaps = new Bitmap[size];

		for (int i = 0; i < size; i++) {
			CacheImageProtocol imgProtocol = new CacheImageProtocol(
					getContext(), newMagazines.magazines.get(i).cover_img);
			imgProtocol.startTrans(new ImgReceiver(i));
		}

		ListView list = (ListView) findViewById(R.id.magazine_list);
		if (adapter == null){
			adapter = new BaseAdapter() {

				@Override
				public View getView(int position, View convertView, ViewGroup parent) {
					View view = View.inflate(getContext(), R.layout.v_list_item,
							null);
					Periodical maga = newMagazines.magazines.get(position);
					ImageView img = (ImageView) view.findViewById(R.id.cover);
					String imgUrl = newMagazines.magazines.get(position).cover_img;
					if (imgUrl != null) {
						CacheImageProtocol imgProtocol = new CacheImageProtocol(
								getContext(), imgUrl);
						imgProtocol.startTrans(img);
					}

					TextView name = (TextView) view.findViewById(R.id.name);
					name.setText(maga.name);

					TextView date = (TextView) view.findViewById(R.id.date);
					date.setText(maga.number);

					TextView intro = (TextView) view.findViewById(R.id.intro);
					intro.setText(maga.description);
					return view;
				}

				@Override
				public long getItemId(int position) {
					return 0;
				}

				@Override
				public Object getItem(int position) {
					return null;
				}

				@Override
				public int getCount() {
					return newMagazines.magazines.size();
				}
			};
			list.setAdapter(adapter);
			list.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					new DownloadMagazine(NewPeriodicalsWidget.this.parent, newMagazines.magazines.get(position));
				}
			});
		}
		adapter.notifyDataSetChanged();
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				refreshUI((NewPeriodicals) msg.obj);
			} else if (msg.what == 2) {
				progressDialog.dismiss();
			} else if (msg.what == 3) {
				NewMagazineCoverflow coverflow = new NewMagazineCoverflow(parent, bitmaps, newMagazines);
				coverflow.showAtLocation(parent.findViewById(R.id.root), Gravity.CENTER, 0, 0);
			}
		};
	};

	private class ImgReceiver implements CacheImageProtocol.Reciever {
		private int index;

		public ImgReceiver(int index) {
			this.index = index;
		}

		@Override
		public void onRecieve(Bitmap bitmap) {
			bitmaps[index] = bitmap;
//Log.d("-----------OnReceive", index+ "");
			boolean complete = true;
			for (int i = 0; i < bitmaps.length; i++) {
				if (bitmaps[i] == null) {
					complete = false;
					break;
				}
			}
			if (complete) {
				handler.sendEmptyMessage(3);
			}
		}
	}
}
